/**
 * 
 */
package org.jenkon.commons.reflect.builder;

import java.lang.reflect.Field;

import org.jenkon.commons.collection.FilterableCollection;
import org.jenkon.commons.collection.utils.CollectionUtils;
import org.jenkon.commons.lang.builder.ToStringBuilder;
import org.jenkon.commons.lang.filter.ReverseFilter;
import org.jenkon.commons.reflect.filter.IsStaticFieldFilter;

/**
 * @author Jens Konopka
 * 
 */
public class GenericToStringBuilder {

	private final Object obj;

	/**
	 * @param obj
	 */
	public GenericToStringBuilder(final Object obj) {
		this.obj = obj;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {

		final ToStringBuilder sb = new ToStringBuilder(obj);

		try {

			final Class<? extends Object> clz = obj.getClass();

			final FilterableCollection<Field> fields = new FilterableCollection<Field>(CollectionUtils.toCollection(clz
					.getDeclaredFields()));

			for (final Field field : fields.filter(new ReverseFilter<Field>(new IsStaticFieldFilter()))) {

				field.setAccessible(true);
				
				sb.append(field.getName(), field.get(obj));
			}

			System.out.println(sb.toString());

		} catch (final IllegalAccessException e) {
			
		}

		return sb.toString();

	}

}
